﻿Public Class ViewDashboardGridDefinition

    Private _viewModelDashboard As ViewModelDashboard
    Private _splitter As Windows.Controls.GridSplitter
    Private _index As Integer
    Private _isrow As Boolean

    Sub New(viewModelDashboard As ViewModelDashboard, splitter As Windows.Controls.GridSplitter, index As Integer, isrow As Boolean)
        ' TODO: Complete member initialization 
        InitializeComponent()

        TypeTailleCombobox.ItemsSource = New String() {"Automatique", "Absolue", "Proportionnelle"}

        _viewModelDashboard = viewModelDashboard
        _splitter = splitter
        _index = index
        _isrow = isrow

        CouloirPrecedentCheckBox.IsChecked = True
        ActiveRadioButton.IsChecked = True

        If isrow Then
            TypeCouloirTextBlock.Text = "Ligne"
        Else
            TypeCouloirTextBlock.Text = "Colonne"
        End If
    End Sub

    Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

    End Sub

    Private Sub CouloirPrecedentCheckBox_Checked(sender As Object, e As Windows.RoutedEventArgs) Handles CouloirPrecedentCheckBox.Checked
        If _isrow Then
            GatherData(_viewModelDashboard.EditingGrid.RowDefinitions(Windows.Controls.Grid.GetRow(_splitter) - 1))
        Else
            GatherData(_viewModelDashboard.EditingGrid.ColumnDefinitions(Windows.Controls.Grid.GetColumn(_splitter) - 1))
        End If
    End Sub

    Private Sub CouloirSuivantCheckBox_Checked(sender As Object, e As Windows.RoutedEventArgs) Handles CouloirSuivantCheckBox.Checked
        If _isrow Then
            GatherData(_viewModelDashboard.EditingGrid.RowDefinitions(Windows.Controls.Grid.GetRow(_splitter) + 1))
        Else
            GatherData(_viewModelDashboard.EditingGrid.ColumnDefinitions(Windows.Controls.Grid.GetColumn(_splitter) + 1))
        End If
    End Sub

    Private Sub GatherData(RowDefinition As Windows.Controls.RowDefinition)
        TailleAbsolueTextBlock.Text = RowDefinition.ActualHeight.ToString("N0")
        TailleMinTextBlock.Text = RowDefinition.MinHeight.ToString("N0")
        TailleMaxTextBlock.Text = RowDefinition.MaxHeight.ToString("N0")

        TailleTextBlock.Text = RowDefinition.Height.ToString()

        TypeTailleCombobox.SelectedIndex = CInt(RowDefinition.Height.GridUnitType)
    End Sub

    Private Sub GatherData(ColumnDefinition As Windows.Controls.ColumnDefinition)
        TailleAbsolueTextBlock.Text = ColumnDefinition.ActualWidth.ToString("N0")
        TailleMinTextBlock.Text = ColumnDefinition.MinWidth.ToString("N0")
        TailleMaxTextBlock.Text = ColumnDefinition.MaxWidth.ToString("N0")

        TailleTextBlock.Text = ColumnDefinition.Width.ToString()

        TypeTailleCombobox.SelectedIndex = CInt(ColumnDefinition.Width.GridUnitType)
    End Sub

    Private Sub ApplyData(RowDefinition As Windows.Controls.RowDefinition)
        If TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Auto) Then
            RowDefinition.Height = Windows.GridLength.Auto
        ElseIf TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Pixel) Then
            If IsNumeric(TailleTextBlock.Text) Then
                RowDefinition.Height = New Windows.GridLength(CInt(TailleTextBlock.Text), Windows.GridUnitType.Pixel)
            End If
        ElseIf TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Star) Then
            If IsNumeric(TailleTextBlock.Text) Then
                RowDefinition.Height = New Windows.GridLength(CDbl(TailleTextBlock.Text) / 100.0, Windows.GridUnitType.Star)
            End If
        End If
    End Sub

    Private Sub ApplyData(ColumnDefinition As Windows.Controls.ColumnDefinition)
        If TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Auto) Then
            ColumnDefinition.Width = Windows.GridLength.Auto
        ElseIf TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Pixel) Then
            If IsNumeric(TailleTextBlock.Text) Then
                ColumnDefinition.Width = New Windows.GridLength(CInt(TailleTextBlock.Text), Windows.GridUnitType.Pixel)
            End If
        ElseIf TypeTailleCombobox.SelectedIndex = CInt(Windows.GridUnitType.Star) Then
            If IsNumeric(TailleTextBlock.Text) Then
                ColumnDefinition.Width = New Windows.GridLength(CDbl(TailleTextBlock.Text) / 100.0, Windows.GridUnitType.Star)
            End If
        End If
    End Sub


    Private Sub CancelButton_Click(sender As Object, e As Windows.RoutedEventArgs) Handles CancelButton.Click
        Windows.Window.GetWindow(Me).Close()
    End Sub

    Private Sub ValidateButton_Click(sender As Object, e As Windows.RoutedEventArgs) Handles ValidateButton.Click
        If ActiveRadioButton.IsChecked Then
            _splitter.IsEnabled = True
            _splitter.Visibility = Windows.Visibility.Visible
        End If
        If BloqueRadioButton.IsChecked Then
            _splitter.IsEnabled = False
            _splitter.Visibility = Windows.Visibility.Visible
        End If
        If MasqueRadioButton.IsChecked Then
            _splitter.IsEnabled = False
            _splitter.Visibility = Windows.Visibility.Collapsed
        End If

        If CouloirPrecedentCheckBox.IsChecked Then
            If _isrow Then
                ApplyData(_viewModelDashboard.EditingGrid.RowDefinitions(Windows.Controls.Grid.GetRow(_splitter) - 1))
            Else
                ApplyData(_viewModelDashboard.EditingGrid.ColumnDefinitions(Windows.Controls.Grid.GetColumn(_splitter) - 1))
            End If
        End If
        If CouloirSuivantCheckBox.IsChecked Then
            If _isrow Then
                ApplyData(_viewModelDashboard.EditingGrid.RowDefinitions(Windows.Controls.Grid.GetRow(_splitter) + 1))
            Else
                ApplyData(_viewModelDashboard.EditingGrid.ColumnDefinitions(Windows.Controls.Grid.GetColumn(_splitter) + 1))
            End If
        End If

        Windows.Window.GetWindow(Me).Close()
    End Sub

End Class
